package com.ctvit.sjk.knowledge.resource.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ctvit.sjk.knowledge.core.enums.QueryTypeEnum;
import com.ctvit.sjk.knowledge.core.controller.KnowledgeController;
import com.ctvit.sjk.knowledge.core.page.TableRecordInfo;
import com.ctvit.sjk.knowledge.resource.pojo.domain.TabFunctionLib;
import com.ctvit.sjk.knowledge.resource.pojo.vo.FunctionLibGetVO;
import com.ctvit.sjk.knowledge.resource.service.ITabFunctionLibService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 函数库Controller
 *
 * @author heyingcheng
 * @date 2025/3/22
 */
@RestController
@RequestMapping("knowledge/function_lib")
public class TabFunctionLibController extends KnowledgeController {

    private final ITabFunctionLibService tabFunctionLibService;

    public TabFunctionLibController(ITabFunctionLibService tabFunctionLibService) {
        this.tabFunctionLibService = tabFunctionLibService;
    }

    /**
     * 查询函数库列表
     */
    @GetMapping("{pageNum}/{pageSize}")
    public TableRecordInfo list(@PathVariable int pageNum,
                                @PathVariable int pageSize,
                                @RequestParam(value = "select_user_id", defaultValue = "all") String type,
                                @RequestParam(defaultValue = "") String name) {
        LambdaQueryWrapper<TabFunctionLib> wrapper = new LambdaQueryWrapper<>();
        if (QueryTypeEnum.MY.getCode().equals(type)) {
            wrapper.eq(TabFunctionLib::getCreateId, CREATE_ID);
        }
        wrapper.like(StringUtils.isNotBlank(name), TabFunctionLib::getFunctionLibName, name);
        wrapper.orderByDesc(TabFunctionLib::getUpdateTime);

        startPage(pageNum, pageSize);
        List<TabFunctionLib> list = tabFunctionLibService.list(wrapper);
        return getRecordTable(list.stream().map(FunctionLibGetVO::new).collect(Collectors.toList()), pageNum, pageSize);
    }


    /**
     * 查询函数库列表
     */
    @PostMapping("list")
    public TableDataInfo list() {
        return getDataTable(tabFunctionLibService.list());
    }

    /**
     * 获取函数库详细信息
     */
    @PostMapping(value = "get")
    public AjaxResult get(@RequestParam String id) {
        return success(tabFunctionLibService.getById(id));
    }

    /**
     * 新增函数库
     */
    @Log(title = "函数库", businessType = BusinessType.INSERT)
    @PostMapping(value = "add")
    public AjaxResult add(@RequestBody TabFunctionLib entity) {
        return toAjax(tabFunctionLibService.save(entity));
    }

    /**
     * 修改函数库
     */
    @Log(title = "函数库", businessType = BusinessType.UPDATE)
    @PostMapping(value = "edit")
    public AjaxResult edit(@RequestBody TabFunctionLib entity) {
        return toAjax(tabFunctionLibService.updateById(entity));
    }

    /**
     * 删除函数库
     */
    @Log(title = "函数库", businessType = BusinessType.DELETE)
    @PostMapping("remove")
    public AjaxResult remove(@RequestBody List<String> ids) {
        return toAjax(tabFunctionLibService.removeByIds(ids));
    }

}
